#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<thread>
#include<windows.h>
#include<mutex>
#include<atomic>
#include<condition_variable>
using namespace std;

condition_variable cv;
bool flag = true;
bool Flag()
{
	return flag;
}

bool UFlag()
{
	return !flag;
}


void func(mutex& mtx)
{
	unique_lock<mutex> lck(mtx);
	for (int i = 0; i < 100; i+=2)
	{
	
		cv.wait(lck, Flag);
		cout << this_thread::get_id() << " : " << i << endl;

		flag = false;
		cv.notify_one();
	}
}

void func2(mutex& mtx)
{
	unique_lock<mutex> lck(mtx);
	for (int i = 1; i < 100; i += 2)
	{
	
		cv.wait(lck, UFlag);
		cout << this_thread::get_id() << " : " << i << endl;
	
		flag = true;
		cv.notify_one();
	}
}


int main()
{
	mutex mtx;
	thread t1 = thread(func ,ref(mtx));
	thread t2 = thread(func2, ref(mtx));
	t1.detach();
	t2.join();

	return 0;
}